package com.shanghaionstar.discover;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.DiscoverActivity;
import com.shanghaionstar.discover.iview.IWifiHomeView;
import com.shanghaionstar.discover.model.WifiInfoModel;
import com.shanghaionstar.discover.presenter.WifiHomePresenter;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.report.bean.ReportReqParam;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.JoinMeConstants;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 7828wc on 16/7/15.
 */
public class WifiHomeFragment extends BaseFragment implements IWifiHomeView {
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private CustomLineLayout ssid_layout, password_layout;

    private WifiHomePresenter presenter;

    private DiscoverActivity activity;

    @Override
    public int getLayoutId() {
        return R.layout.discover_wifi_home;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFirstLoad()) {
            activity = (DiscoverActivity) getActivity();

            btnBackClick(top_layout);
            presenter = new WifiHomePresenter(((DiscoverActivity) getActivity()).getScheduler());
            presenter.setView(this);
            presenter.registerRxBus();


            presenter.hotSpotInfo("", "", RemoteCommandsType.COMMAND_GETHOTSPOTINFO);
        }


        if (ApplicationUtil.getInstance().getLoginRole().equalsIgnoreCase(JoinMeConstants.OWNER)) {
            ssid_layout.showRightArrow(true);
            password_layout.showRightArrow(true);
        } else {
            ssid_layout.showRightArrow(false);
            password_layout.showRightArrow(false);
        }
    }

    @Override
    public void setLoading(boolean isLoading) {
        ssid_layout.showLoading(isLoading);
        password_layout.showLoading(isLoading);
    }

    @Override
    public void onResponseError(OnStarException e) {
        BusinessUtil.sendReportByRequestParams(activity, FunctionNameFindId.Find_VehicularWiFi.getStringValue(), ReportReqParam.getWIFIInfoReqParm(false, e.getMessage()));
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
        ssid_layout.showRightArrow(false);
        password_layout.showRightArrow(false);
    }

    @Override
    public void onReponseSuccess(final WifiInfoModel object) {
        if (object != null) {
            ssid_layout.setRightText(object.getSsid());
            password_layout.setRightText(object.getPassphrase());

            BusinessUtil.sendReportByRequestParams(activity, FunctionNameFindId.Find_VehicularWiFi.getStringValue(), ReportReqParam.getWIFIInfoReqParm(true));
            if (ApplicationUtil.getInstance().getLoginRole().equalsIgnoreCase(JoinMeConstants.OWNER)) {
                ssid_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BusinessUtil.sendReportByClickType(getContext(), FunctionNameFindId.WIFIchangessid.getStringValue());
                        Bundle bundle = new Bundle();
                        bundle.putString("ssid", ssid_layout.getRightText());
                        bundle.putBoolean("isUpdateSSID", true);
                        bundle.putString("password", password_layout.getRightText());
                        bundle.putString(FinalConstants.JUMP_FLAG, WifiDetailFragment.class.getName());
                        WifiDetailFragment fragment = new WifiDetailFragment();
                        fragment.setArguments(bundle);
                        activity.changeFragment(fragment, WifiDetailFragment.class, false);
                    }
                });
                password_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BusinessUtil.sendReportByClickType(getContext(), FunctionNameFindId.WIFIChoose.getStringValue());
                        Bundle bundle = new Bundle();
                        bundle.putBoolean("isUpdateSSID", false);
                        bundle.putString("ssid", ssid_layout.getRightText());
                        bundle.putString("password", password_layout.getRightText());
                        bundle.putString(FinalConstants.JUMP_FLAG, WifiDetailFragment.class.getName());
                        WifiDetailFragment fragment = new WifiDetailFragment();
                        fragment.setArguments(bundle);
                        activity.changeFragment(fragment, WifiDetailFragment.class, false);
                    }
                });
            }

        }
    }

    @Override
    public void verifyResult(int errorResId, boolean isPassword) {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (activity instanceof DiscoverActivity) {
            if (!TextUtils.isEmpty(activity.getSsid())) {
                ssid_layout.setRightText(activity.getSsid());
            }
            if (!TextUtils.isEmpty(activity.getWifiPassword())) {
                password_layout.setRightText(activity.getWifiPassword());
            }
        }
    }

    @Override
    public void onBack() {
        activity.finish();
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.stopPollingService(RemoteCommandsType.COMMAND_GETHOTSPOTINFO);
    }
}
